{extend name="$index_style_layout" /}
{block name="head_title"}{:M('name')} 频道主页{/block}
{block name="body_content"}
<link rel="stylesheet" href="__STATIC__/bbs/pcpost.css?a">
<div class="MainContainer">
	<div class="qideBox"><a href="{:urls('index/index')}">社区主页</a> -&gt; {notempty name="fid"}<a href="{:urls('content/index',['fid'=>$fid])}">{:get_sort($fid)}</a> &gt;{/notempty} 发表帖子</div>
	<form class="ajax-post" action="" method="post">
	<div class="FormBox">
		{:hook_listen('template_form_head',$info,null,true)}
		
		<dl>
			<dt><input type="text" value="{$info.title}" name="title" placeholder="请输入标题" required/></dt>
			<dd>
				<select name="fid">
					<option value="0">请选择栏目</option>
					{volist name="''|sort_config" id="rs"}
					<option value="{$rs.id}" {eq name="$fid" value="$rs.id"}selected{/eq}>{$rs.title_prefix}{$rs.name}</option>
					{/volist}
				</select>
			</dd>
{if (!isset($webdb['M__qun']['modules_show_select_qun'])||in_array(config('system_dirname'),$webdb['M__qun']['modules_show_select_qun']))&&((empty($id)&&empty($ext_id))||$id)&&getArray($qun=fun('qun@myjoin'))}
			<dd>
				<select name="ext_id">
					<option value="{$info.ext_id|default=0}">选择__QUN__</option>
					{volist name="$qun" id="rs"}
					<option value="{$rs.id}" {eq name="$info.ext_id" value="$rs.id"}selected{/eq}>{$rs.title}</option>
					{/volist}
				</select>
			</dd>
{/if}

{if (!isset($webdb['M__qun']['modules_show_select_topic'])||in_array(config('system_dirname'), $webdb['M__qun']['modules_show_select_topic']) )&&config('is_biz')&&empty($info)&&$qun=fun('qun@myjoin')}
			<dd>
				<select name="topic_aid">
					<option value="{$info.ext_id|default=0}">归属专题</option>
					{volist name="$qun" id="rs"}
					{if $rs.uid==$userdb.uid}
					<option value="{$rs.id}" {eq name="$info.ext_id" value="$rs.id"}selected{/eq}>{$rs.title}</option>
					{/if}
					{/volist}
				</select>
			</dd>
{/if}

		</dl>
{if plugins_config('kwtags')}
<style type="text/css">
.choose_tags .t{
	margin-left:10px;
	color:green;
}
.choose_tags .tags a{
	background:#ddd;
	cursor:pointer;
	padding:2px 5px;
	margin-left:5px;
}
</style>
		<dl style="margin-top:12px;">
			<dt>
				<input type="text" value="{$info.kwtags}" style="width:400px;" name="kwtags" placeholder="自定义话题关键字,多个用半角逗号隔开"/>
				<select id="offen_tag" style="height:35px;margin-left:10px;">
					<option value="">常用关键词</option>
					{volist name=":fun('Tagfun@tagname',30)" id="rs"}
					<option value="{$rs.tagname}">{$rs.tagname}</option>
					{/volist}
				</select>
				<em class="choose_tags"><span class="t">可选关键字：</span><span class="tags"></span></em>
			</dt>
		</dl>
<script type="text/javascript">
$(function(){
	var obj = $(".ajax-post input[name='kwtags']");
	$("#offen_tag").change(function(){
		var word = $(this).val();
		if(obj.val().indexOf(word)==-1){
			var str = obj.val()==''?word:obj.val()+','+word;
			obj.val(str);
			$(this).find("option[value='"+word+"']").remove();
		}else{
			layer.msg("不能重复选择");
		}
	});
	$(".ajax-post input[name='title']").blur(function(){
		$.get("{:purl('kwtags/api/getword')}?title="+$(this).val(),function(res){
			if(res.code==0){
				var ar = res.data[0].split(',');
				$(".choose_tags .tags").html('<a title="点击选择">'+ar.join('</a><a title="点击选择">')+'</a>');				
				$(".choose_tags .tags a").click(function(){		
					var word = $(this).html();
					if(obj.val().indexOf(word)==-1){
						var str = obj.val()==''?word:obj.val()+','+word;
						obj.val(str);
						$(this).css('color','#999');
					}else{
						layer.msg("不能重复选择");
					}
				});
			}
		});
	})
});
</script>
{/if}
		<div class="ueditor">
<script id="content" class="js-ueditor" name="content" type="text/plain">{$info.content}</script>
{include file="index@editor/btn" /}
<div class="slectEditMode"><a href="javascript:showEditMode();">布局模板</a> <a href="javascript:add_other_mv();">站外视频</a> <a href="javascript:add_topic();">站内引用</a> <a href="javascript:ueditor.execCommand('insertHtml','[reply] 请在这括号范围内输入要隐藏的内容 [/reply]')" style="background:orange;">设置回复可见</a> <a href="javascript:set_group_view()" style="background:orange;">用户组权限</a> <a href="javascript:set_password_view()" style="background:orange;">设置密码访问</a> <a href="javascript:set_pay_view()" style="background:orange;">付费或圈内访问</a><!-- <a href="javascript:set_paymoney_view()" style="background:orange;">积分消费权限</a>  <a href="javascript:ueditor.execCommand('insertHtml','[qun] 请在这括号范围内输入要隐藏的内容 [/qun]')" style="background:orange;">仅圈内成员可见</a>--></div>
{include file="content/post_set" /}<!--浏览权限功能-->
<script type="text/javascript">
var ueditor;	//内容布局模板要用到
jQuery(document).ready(function() {
	//重新定义编辑器的宽度＝表单提交容器标签的宽度
	$('#PostComment .ueditor').width($('#PostComment .submit').width());   
    ueditor =  UE.getEditor($('.js-ueditor').attr('name'), {
            initialFrameHeight:300,  //初始化编辑器高度,默认320
            autoHeightEnabled:false,  //是否自动长高
            maximumWords: 50000, //允许的最大字符数
            serverUrl: '{:urls("index/attachment/upload","dir=images&from=ueditor&module=bbs")}',			 
        }); 
});
</script>
<script src="__STATIC__/libs/ueditor/ueditor.config.js"></script>
<script src="__STATIC__/libs/ueditor/ueditor.all.min.js"></script>
		</div>
		<ul class="uploadImg">
			<div style="display:none;">
				<input type="file" name="fileToUpload" id="fileToUpload" accept="image/*" multiple/>
				<input type="text" name="picurl" value="{$info.picurl}" id="compressValue"  style="width:100%;" /> 
			</div>
			<ol>点击上传图片</ol>
			<li onclick='$("#fileToUpload").click()'><i class="si si-camera"></i></li>
		</ul>
		<div class="ListImgs">
		</div>
		{:hook_listen('template_form_foot',$info,null,true)}
		<div class="button"><button type="submit">{empty name='id'}发表帖子{else /}修改帖子{/empty}</button></div>	
	</div>
	</form>
</div>

<script type="text/javascript" src="__STATIC__/js/base64uppic.js"></script>
<script type="text/javascript">
		var severUrl = "{:urls('index/attachment/upload','dir=images&from=base64&module=bbs')}";
		var pics = [];
        //图片上传change事件
        $('#fileToUpload').change(function(){			
            uploadBtnChange($(this).attr("id"),'compressValue',pics,viewpics);
        });

		function viewpics(url,pic_array){
			var html = '';
			pic_array.forEach(function(f){
				if(f.indexOf('://')==-1 && f.indexOf('/public/')==-1){
					f = '/public/'+f;
				}
				html += '<div><span><img src="'+f+'"></span><em class="del"><i class="fa fa-minus-circle"></i></em><em class="insert"><i class="fa fa-plus-circle"></i></em></div>';
			});
			$(".ListImgs").html(html);
			del_pic();
			insert_pic();
		}
		if($("#compressValue").val()!='')pics = $("#compressValue").val().split(',');
		viewpics('',pics);		
		

		function del_pic(){
			$(".ListImgs .del").click(function(e){
				//这里删除的图片没有真正从服务器删除
				$(this).parent().remove();
				pics = [];
				var obj = $(".ListImgs img");
				obj.each(function(e){
					var img = $(this).attr("src");
					img = img.replace('/public/','');
					pics.push(img);
					$("#compressValue").val( pics.join(',') );
				});
				if(obj.length==0){
					$("#compressValue").val('');
				}			
			});
		}
		//del_pic();

		function insert_pic(){	//往文中插入组图
			$(".ListImgs .insert").click(function(e){
				var url = $(this).parent().find("img").attr('src');
				ueditor.execCommand('insertHtml', '<br><img src="'+url+'"><br><br><br>');
			});
		}
		//insert_pic();

</script>

<SCRIPT LANGUAGE='JavaScript'>

function postform(){
	layer.confirm('你确认要提交吗?',{
            btn:['确定','取消']
        },function(index){
			var form_data = $('.ajax-post').serialize();
			postcontent(form_data);
        }
	);
}

var havepost = false;
function postcontent(form_data){
			var that  = ueditor;
			if(that.hasContents()==false){
				layer.alert('内容不能为空')
				return false;
			}
			if(havepost==true){
				layer.msg('请不要重复提交');
				return false;
			}
			havepost = true;
            $.post(url, form_data).success(function (res) {
                if(res.code==0){
					layer.msg("{empty name='id'}发表成功！{else /}修改成功{/empty}");
					location.href="{:urls('content/show')}?id=" + res.data.id;
				}else{
					havepost = false;
					layer.alert('提交失败:' + res.msg);
				}
            }).fail(function () {
				havepost = false;
				layer.open({title: '提示',content: '服务器发生错误'});
            });
}

var url = "{empty name='id'}{:urls('wxapp.post/add',['ext_sys'=>$ext_sys,'ext_id'=>$ext_id])}{else /}{:urls('wxapp.post/edit',['id'=>$id])}{/empty}";

$(document).ready(function(){
        $('.ajax-post').submit(function () {
            var form_data = $(this).serialize();
			postcontent(form_data)
            return false;
        });
 });

//记录上次选择的哪个圈子
$("select[name=ext_id]").change(function(){
	$.cookie('choose_qun_id', $(this).val(), { expires: 60*24*30, path: '/' });
});
$(document).ready(function(){
	var value = $.cookie('choose_qun_id');
	if(value>0  && parseInt("{$id|intval}")<1){
		$("select[name=ext_id] option[value='"+value+"']").attr("selected",true)
	}
});
</SCRIPT>
{/block}